﻿Imports System
Imports System.Text
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
Imports System.Workflow.Activities.Rules

Namespace System.Workflow.Activities.Rules.Design
    Friend Class DesignerHelpers
        ' Methods
        Friend Shared Sub DisplayError(ByVal message As String, ByVal messageBoxTitle As String, ByVal serviceProvider As IServiceProvider)
            Dim service As IUIService = Nothing
            If (Not serviceProvider Is Nothing) Then
                service = DirectCast(serviceProvider.GetService(GetType(IUIService)), IUIService)
            End If
            If (Not service Is Nothing) Then
                service.ShowError(message)
            Else
                MessageBox.Show(message, messageBoxTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, 0)
            End If
        End Sub

        Friend Shared Function GetRulePreview(ByVal rule As Rule) As String
            Dim builder As New StringBuilder
            If (Not rule Is Nothing) Then
                builder.Append("IF ")
                If (Not rule.Condition Is Nothing) Then
                    builder.Append((rule.Condition.ToString & " "))
                End If
                builder.Append("THEN ")
                Dim action As RuleAction
                For Each action In rule.ThenActions
                    builder.Append(action.ToString)
                    builder.Append(" "c)
                Next
                If (rule.ElseActions.Count > 0) Then
                    builder.Append("ELSE ")
                    Dim action2 As RuleAction
                    For Each action2 In rule.ElseActions
                        builder.Append(action2.ToString)
                        builder.Append(" "c)
                    Next
                End If
            End If
            Return builder.ToString
        End Function

        Friend Shared Function GetRuleSetPreview(ByVal ruleSet As RuleSet) As String
            Dim builder As New StringBuilder
            Dim flag As Boolean = True
            If (Not ruleSet Is Nothing) Then
                Dim rule As Rule
                For Each rule In ruleSet.Rules
                    If flag Then
                        flag = False
                    Else
                        builder.Append(ChrW(10))
                    End If
                    builder.Append(rule.Name)
                    builder.Append(": ")
                    builder.Append(DesignerHelpers.GetRulePreview(rule))
                Next
            End If
            Return builder.ToString
        End Function

    End Class
End Namespace

